Use openssl on Unix rather than C functions directly for SHA256
authorJohn Ericson <Ericson2314@Yahoo.com>
Mon, 1 Aug 2016 20:28:25 +0000 (13:28 -0700)
committerJohn Ericson <Ericson2314@Yahoo.com>
Mon, 1 Aug 2016 20:28:25 +0000 (13:28 -0700)
Cargo.lock
Cargo.toml
src/cargo/util/sha256.rs

index dc6fcb967b8cbd7717c66eac71e78a53c5a757db..93c055af5ebc1b59613e92a6eb10f309dddfdb5c 100644 (file)
@@ -23,6 +23,7 @@ dependencies = [
  "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "miow 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "openssl 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "regex 0.1.58 (registry+https://github.com/rust-lang/crates.io-index)",
  "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
  "semver 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -56,6 +57,11 @@ name = "bitflags"
 version = "0.1.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "bitflags"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "bufstream"
 version = "0.1.1"
@@ -244,6 +250,11 @@ dependencies = [
  "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "lazy_static"
+version = "0.1.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "lazy_static"
 version = "0.2.1"
@@ -368,6 +379,19 @@ dependencies = [
  "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "openssl"
+version = "0.7.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "openssl-sys 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "openssl-sys-extras 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "openssl-sys"
 version = "0.7.8"
@@ -380,6 +404,16 @@ dependencies = [
  "user32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "openssl-sys-extras"
+version = "0.7.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "openssl-sys 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "pkg-config"
 version = "0.3.8"
index c731a81e2b9276219981a1ac5085b8a4e2a34d61..db6c8d6dfb5a2667e3823f27fdca36a63959deb1 100644 (file)
@@ -45,6 +45,9 @@ toml = "0.1.29"
 url = "1.1"
 winapi = "0.2"
 
+[target.'cfg(unix)'.dependencies]
+openssl = "0.7"
+
 [dev-dependencies]
 hamcrest = "0.1"
 bufstream = "0.1"
index 18a258af3161be9f001be494cfe6ea889f8c30af..97bee1c357f78edd0098c4dfb79a43b466e6c10c 100644 (file)
@@ -6,63 +6,26 @@ pub use self::imp::Sha256;
 // allow improper ctypes because size_t falls under that in old compilers
 #[allow(bad_style, improper_ctypes)]
 mod imp {
-    use libc;
+    extern crate openssl;
 
-    enum EVP_MD_CTX {}
-    enum EVP_MD {}
-    enum ENGINE {}
+    use std::io::Write;
+    use self::openssl::crypto::hash::{Hasher, Type};
 
-    extern {
-        fn EVP_DigestInit_ex(ctx: *mut EVP_MD_CTX,
-                             kind: *const EVP_MD,
-                             imp: *mut ENGINE) -> libc::c_int;
-        fn EVP_DigestUpdate(ctx: *mut EVP_MD_CTX,
-                            d: *const libc::c_void,
-                            cnt: libc::size_t) -> libc::c_int;
-        fn EVP_DigestFinal_ex(ctx: *mut EVP_MD_CTX, md: *mut libc::c_uchar,
-                              s: *mut libc::c_uint) -> libc::c_int;
-        fn EVP_MD_CTX_create() -> *mut EVP_MD_CTX;
-        fn EVP_MD_CTX_destroy(ctx: *mut EVP_MD_CTX);
-        fn EVP_sha256() -> *const EVP_MD;
-    }
-
-    pub struct Sha256 { ctx: *mut EVP_MD_CTX }
+    pub struct Sha256(Hasher);
 
     impl Sha256 {
         pub fn new() -> Sha256 {
-            unsafe {
-                let ctx = EVP_MD_CTX_create();
-                assert!(!ctx.is_null());
-                let ret = Sha256 { ctx: ctx };
-                let n = EVP_DigestInit_ex(ret.ctx, EVP_sha256(), 0 as *mut _);
-                assert_eq!(n, 1);
-                ret
-            }
+            Sha256(Hasher::new(Type::SHA256))
         }
 
         pub fn update(&mut self, bytes: &[u8]) {
-            unsafe {
-                let n = EVP_DigestUpdate(self.ctx, bytes.as_ptr() as *const _,
-                                         bytes.len() as libc::size_t);
-                assert_eq!(n, 1);
-            }
+            let _ = self.0.write_all(bytes);
         }
 
         pub fn finish(&mut self) -> [u8; 32] {
-            unsafe {
-                let mut ret = [0u8; 32];
-                let mut out = 0;
-                let n = EVP_DigestFinal_ex(self.ctx, ret.as_mut_ptr(), &mut out);
-                assert_eq!(n, 1);
-                assert_eq!(out, 32);
-                ret
-            }
-        }
-    }
-
-    impl Drop for Sha256 {
-        fn drop(&mut self) {
-            unsafe { EVP_MD_CTX_destroy(self.ctx) }
+            let mut ret = [0u8; 32];
+            ret.copy_from_slice(&self.0.finish()[..]);
+            ret
         }
     }
 }